package api

import (
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	fromData "user_api/api_structure"
	"user_api/global"
	proto "user_api/proto"
	myvalidator "user_api/validator"
)

//根据角色ID查询角色信息
// @Summary 根据角色ID查询角色信息
// @Description 描述信息
// @Tags role
// @Param role_id query int true "角色ID"
// @Success 200 {object} global.Res {'code':200,'msg':'','data':null}
// @Failure 400 {object} global.Res {'code':400,'msg':'','data':null}
// @Router /v1/role/getRoleInfoByRoleId [post]

func GetRoleInfoByRoleId(ctx *gin.Context) {

	registerForm := fromData.GetRoleIdForm{}
	if err := ctx.ShouldBind(&registerForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}
	req := &proto.RequestRoleId{RoleId: registerForm.RoleId}

	res, err := global.UserSrvClient.GetRoleInfoByRoleId(context.TODO(), req)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return
}

//添加角色
func AddRole(ctx *gin.Context) {
	//获取角色信息数据结构
	rep := &proto.ResponseRoleInfo{}
	//获取通过api接口传输的数据结构
	AddRoleData := fromData.AddRoleForm{}

	if err := ctx.ShouldBind(&AddRoleData); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}
	if err := global.SwapTo(AddRoleData, rep); err != nil {
		return
	}

	res, err := global.UserSrvClient.AddRole(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return
}

//编辑角色
func EditRole(ctx *gin.Context) {
	rep := &proto.ResponseRoleInfo{}
	editRoleForm := fromData.AddRoleForm{}

	if err := ctx.ShouldBind(&editRoleForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}
	if err := global.SwapTo(editRoleForm, rep); err != nil {
		return
	}

	res, err := global.UserSrvClient.EditRole(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return
}

//删除角色
func DelRole(ctx *gin.Context) {
	GetRoleIdForm := fromData.GetRoleIdForm{}

	if err := ctx.ShouldBind(&GetRoleIdForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}

	rep := &proto.RequestRoleId{RoleId: GetRoleIdForm.RoleId}

	zap.S().Debug("传输的参数", rep)

	res, err := global.UserSrvClient.DelRole(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return
}

// @Summary 获取角色列表
// @Description 描述信息
// @Tags role
// @Success 200 {object} global.Res {'code':200,'msg':'','data':null}
// @Failure 400 {object} global.Res {'code':400,'msg':'','data':null}
// @Router /v1/role/GetRoleList [post]
func GetRoleList(ctx *gin.Context) {
	rep := &proto.RequestLimitList{}
	RequestLimitFrom := fromData.RequestLimitFrom{}

	if err := ctx.ShouldBind(&RequestLimitFrom); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}
	if err := global.SwapTo(RequestLimitFrom, rep); err != nil {
		return
	}
	zap.S().Debug("传输的参数", rep)

	res, err := global.UserSrvClient.GetRoleList(context.TODO(), rep)
	zap.S().Debug("结果信息", res)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return

}
